Duplication of Side Effects

Contents|Index|Previous|Next
Duplication of side effects

Many C programs define a macro, min (standing for minimum), like the following examples input.

    #define min(X, Y) ((X) < (Y) ? (X) : (Y))

When you use this macro with an argument containing a side effect, it expands. Consider the following declaration.

    next = min (x + y, foo (z));

The previous statement then could expand into the following declaration.

    next = ((x + y) < (foo (z)) ? (x + y) : (foo (z)));

x + y has been substituted for X and foo (z) for Y in the declaration. The function, foo, is used only once in the statement as it appears in the program, but the expression, foo (z), has been substituted twice into the macro expansion. As a result, foo might be called two times when the statement is executed. If it has side effects or if it takes a long time to compute, the results might not be what you intended. We say that min is an unsafe macro. The best solution to this problem is to define min in a way that computes the value of foo (z) only once. The C language offers no standard way to do this, but it can be done with GNU C extensions as the following example shows.

    #define min(X, Y) \
    ({ typeof (X) __x = (X), __y = (Y); \
       (__x < __y) ? __x : __y; })

If you do not wish to use GNU C extensions, the only solution is to be careful when using the macro, min. For instance, you can calculate the value of foo (z), saving it in a variable, and using that variable in min, as in the following example.

    #define min(X, Y) ((X) < (Y) ? (X) : (Y))

    . . .

    { int tem = foo (z); next = min (x + y, tem); }

This operation assumes that foo returns type, int.

Top|Contents|Index|Previous|Next